import { logger } from '../core/logger';
import { clientBase, HOST } from './clientBase';
import { ajaxResult } from '../models/ajaxResult';
import { viewReport } from '../models/viewReport';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class reportClient extends clientBase {
  getDeviceData(deviceId: string, dateTime: Date): Promise<ajaxResult<Array<viewReport>>> {
    return new Promise((resolve) => {
      let date = `${dateTime.getFullYear()}-${(dateTime.getMonth() + 1)}-${dateTime.getDate()}`;
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/report/getDeviceData?deviceId=${deviceId}&dateTime=${date}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<viewReport> = response.data.result;
          resolve(this.buildSuccessResult<Array<viewReport>>(ret));
        } else
          resolve(this.buildFailedResult<Array<viewReport>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<viewReport>>(err));
      });
    });
  }

  getAreaData(areaCode: string, dateTime: Date): Promise<ajaxResult<Array<viewReport>>> {
    return new Promise((resolve) => {
      let date = `${dateTime.getFullYear()}-${(dateTime.getMonth() + 1)}-${dateTime.getDate()}`;
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/report/getAreaData?areaCode=${areaCode}&dateTime=${date}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<viewReport> = response.data.result;
          resolve(this.buildSuccessResult<Array<viewReport>>(ret));
        } else
          resolve(this.buildFailedResult<Array<viewReport>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<viewReport>>(err));
      });
    });
  }

  getDeviceWeekData(deviceId: string, dateTime: Date): Promise<ajaxResult<Array<viewReport>>> {
    return new Promise((resolve) => {
      let date = `${dateTime.getFullYear()}-${(dateTime.getMonth() + 1)}-${dateTime.getDate()}`;
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/report/getDeviceWeekData?deviceId=${deviceId}&dateTime=${date}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<viewReport> = response.data.result;
          resolve(this.buildSuccessResult<Array<viewReport>>(ret));
        } else
          resolve(this.buildFailedResult<Array<viewReport>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<viewReport>>(err));
      });
    });
  }

  getAreaWeekData(areaCode: string, dateTime: Date): Promise<ajaxResult<Array<viewReport>>> {
    return new Promise((resolve) => {
      let date = `${dateTime.getFullYear()}-${(dateTime.getMonth() + 1)}-${dateTime.getDate()}`;
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/report/getAreaWeekData?areaCode=${areaCode}&dateTime=${date}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<viewReport> = response.data.result;
          resolve(this.buildSuccessResult<Array<viewReport>>(ret));
        } else
          resolve(this.buildFailedResult<Array<viewReport>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<viewReport>>(err));
      });
    });
  }
}
export default new reportClient();